// 2013-7-29下午02:48:59  net.betou.trade.admin.action.CategoryAct.java

package net.betou.trade.admin.action;

import static net.betou.common.page.SimplePage.cpn;
import static net.betou.common.page.SimplePage.cpnPageSize;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.page.Pagination;
import net.betou.common.util.TradeUtils;
import net.betou.trade.action.abs.ActionSupport;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Category;
import net.betou.trade.main.manager.CategoryMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;
import net.betou.trade.web.WebErrors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author 叶装装
 */
@Controller
public class CategoryAct extends ActionSupport< Category >{
	
	private static final String DIRECTORY = "category";
	private static final Logger log = LoggerFactory.getLogger( CategoryAct.class );
	
	@RequestMapping( "/category/v_list.jspx" )
	public String list( Integer pageSize, Integer pageNo,  String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model  ){
		
		pageSize = cpnPageSize( pageSize );
		pageNo = cpn( pageNo );
		
		AgentAndAdmins user = TradeUtils.getAdmin( request ); // 获取当前管理员
		Pagination pagination = manager.getPage( user, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		
		model.addAttribute( "admin", user );
		
		paramToModel( model, pagination, null, pageSize, searchProperty, searchValue, orderProperty, orderDirection, null );
		return getListPage( );
	}
	
	@RequestMapping( "/category/v_add.jspx" )
	public String v_add( HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins user = TradeUtils.getAdmin( request ); // 获取当前管理员
		
		WebErrors errors = validateToSave( request, user );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		return getAddPage( );
	}
	
	@RequestMapping( "/category/v_ajax_add.jspx" )
	public String v_ajax_add( HttpServletRequest request, ModelMap model ){
		AgentAndAdmins user = TradeUtils.getAdmin( request ); // 获取当前管理员
		
		WebErrors errors = validateToSave( request, user );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		return DIRECTORY + "/ajaxadd";
	}
	
	@RequestMapping( "/category/o_ajax_save.jspx" )
	public void o_ajax_save( Category bean, HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		WebErrors errors = validateSave( bean );
		ResponseUtil util = ResponseUtil.create( response );
		
		if( errors.hasErrors( ) ){
			util.addErrorResponse( errors.getError( ) );
			util.renderJson( );
			return;
		}
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		bean = manager.save( bean, admin );
		
		logMng.operating( 0, request, Messages.LOG_CATEGORY, "id=" + bean.getId( ) + ",name=" + bean.getName( ) );
		
		util.addSuccessResponse( bean.getName( ) );
		util.renderJson( );
		return ;
		
	}
	
	@RequestMapping( "/category/o_save.jspx" )
	public String o_save( Category bean,  HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		
		WebErrors errors = validateSave( bean );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		bean = manager.save( bean, admin );
		
		logMng.operating( 0, request, Messages.LOG_CATEGORY, "id=" + bean.getId( ) + ",name=" + bean.getName( ) );
		
		return "redirect:v_list.jspx";
	}
	
	@RequestMapping ( "/category/v_edit.jspx" )
	public String v_edit( Integer id, HttpServletRequest request, ModelMap model ){

		WebErrors errors = validateUpdate( request, id );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		Category bean = manager.findById( id );
		
		model.addAttribute( "bean", bean );
		
		return getUpdatePage( );
	}
	
	@RequestMapping ( "/category/o_update.jspx" )
	public String o_update(  Integer id, Category bean, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		WebErrors errors = validateOUpdate( id, admin.getId( ), admin.getSystem( ) );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		bean = manager.update( bean );
		
		logMng.operating( 1, request, Messages.LOG_CATEGORY, "id=" + bean.getId( ) + ",name=" + bean.getName( ));
		
		return "redirect:v_list.jspx";
	}
	
	
	@RequestMapping( "/category/o_delete.jspx" )
	public void o_delete( Integer [ ] ids, HttpServletResponse response, HttpServletRequest request ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		ResponseUtil util = validateDelete( ids, admin.getId( ), admin.getSystem( ), response, request );

		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}

		Category [ ] beans = manager.deleteByIds( ids );

		for ( Category bean : beans ){
			log.info( "delete Category id={}", bean.getId( ) );
			logMng.operating( 2, request, Messages.LOG_CATEGORY, "id=" + bean.getId( ) + ",name=" + bean.getName( ));
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );
		return;
		
	}
	
	private ResponseUtil validateDelete( Integer [ ] ids, Integer adminId, Boolean system, HttpServletResponse response, HttpServletRequest request ) {
		ResponseUtil util = ResponseUtil.create( response );

		if( ids == null || ids.length == 0 ){
			util.addErrorResponse( Messages.DATA_NOT_SELECT );
			return util;
		}

		for ( Integer id : ids ){
			Category bean = manager.findMyCategory( id, adminId, system );
			if( bean == null ){
				util.addErrorResponse( system ? Messages.DATA_NOT_EXIST : Messages.DATA_NOT_OPERATING );
				break;
			}
		}
		return util;
	}
	
	
	private WebErrors validateOUpdate( Integer id, Integer adminId, Boolean system ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		Category bean = manager.findMyCategory( id, adminId, system );
		
		if( bean == null ){
			errors.addError( Messages.DATA_NOT_OPERATING );
			return errors;
		}
		
		return errors;
		
	}
	
	private WebErrors validateUpdate( HttpServletRequest request, Integer id ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		Category bean = manager.findById( id );
		
		if( bean == null ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		return errors;
			
	}
	
	private WebErrors validateSave( Category bean ){
		WebErrors errors = WebErrors.create( );
		return errors;
	}
	
	private WebErrors validateToSave( HttpServletRequest request, AgentAndAdmins user ){
		
		WebErrors errors = WebErrors.create( );
		
//		if( user.getSystem( ) ){
//			log.info( Messages.CATEGORY_ADMIN_SYSTEM_MANAGER_NOT_TO_ADD );
//			errors.addError( Messages.CATEGORY_ADMIN_SYSTEM_MANAGER_NOT_TO_ADD );
//			return errors;
//		}
		
		return errors;
		
	}
	
	@Override
	protected String getDirectory( ) {
		return DIRECTORY;
	}
	
	@Autowired
	private CategoryMng manager;

}
